package com.keffect.effects;

import java.awt.Graphics2D;
import java.util.Vector;

import com.keffect.KEffect;
import com.keffectpanel.KEffectPanel;

/**
 * This effect provide the ability to combine multiples effects of the class
 * {@link KEffect}. A MultipleEffect will become a new effect as a result of the
 * properties combination of all the KEffect Objects added.
 * <p>
 * 
 * This class maintain a collection of objects of the class {@link KEffect}.
 * When the {@link #start()} operation is invoked, all the effects in the
 * collection will start and be combined. While the effect is running, it will
 * be impossible to modify the collection. To do such thing, it will be
 * necessary first invoke the {@link #stop()} operation.
 * 
 * <p>
 * <b>Note: The order in which the effects are added, may take different
 * results.</b>
 * 
 * @author Matias Varela
 *         <p>
 *         Student of Licenciatura en Ciencias de la Computacion, at the
 *         Universidad Nacional del Sur (Argentina).
 *         <p>
 *         project site: http://code.google.com/p/keffect-project/
 * 
 */
public class MultipleEffect implements KEffect {

	protected Vector<KEffect> effects;
	protected KEffectPanel panel;


	public MultipleEffect() {
		this.effects = new Vector<KEffect>();
	}


	@Override
	public boolean isActive() {
		if (panel != null) {
			for (int i = 0; i < effects.size(); i++) {
				if (effects.get(i).isActive()) return true;
			}
			return false;
		} else return false;
	}


	@Override
	public KEffectPanel getEffectPanel() {
		return this.panel;
	}


	@Override
	public void setEffectPanel(KEffectPanel panel) {
		for (int i = 0; i < effects.size(); i++) {
			effects.get(i).setEffectPanel(panel);
		}
		this.panel = panel;
	}


	@Override
	public void start() {
		for (int i = 0; i < effects.size(); i++) {
			effects.get(i).start();
		}

	}


	@Override
	public void initEffectProperties() {
		// TODO Auto-generated method stub

	}


	@Override
	public void doPaintBefore(Graphics2D g) {
		for (int i = 0; i < effects.size(); i++) {
			effects.get(i).doPaintBefore(g);
		}
	}


	@Override
	public void doPaintAfter(Graphics2D g) {
		for (int i = 0; i < effects.size(); i++) {
			effects.get(i).doPaintAfter(g);
		}
	}


	@Override
	public void stop() {
		for (int i = 0; i < effects.size(); i++) {
			effects.get(i).stop();
		}
	}


	/**
	 * Add a new effect to the collection.
	 * 
	 * @param e
	 *            effect to be added.
	 */
	public void addEffect(KEffect e) {
		if (!this.isActive()) this.effects.add(e);
	}


	/**
	 * Remove an effect from the collection.
	 * 
	 * @param e
	 *            the effect to be removed.
	 * @return <code>true</code> if the operation has been succeed;
	 *         <code>false</code> otherwise.
	 */
	public boolean removeEffect(KEffect e) {
		if (!this.isActive()) return this.effects.remove(e);
		return false;
	}


	/**
	 * Remove all the effects from the collection.
	 * 
	 */
	public void removeAll() {
		if (!this.isActive()) this.effects.removeAllElements();
	}

}
